package com.seveneight.baijiesteward.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.base.MessageBean;
import com.seveneight.baijiesteward.bean.BranchBean;
import com.seveneight.baijiesteward.bean.CardBean;
import com.seveneight.baijiesteward.ui.user.contract.AddCardContract;
import com.seveneight.baijiesteward.ui.user.contract.BranchBankContract;
import com.seveneight.baijiesteward.ui.user.contract.EditCardContract;
import com.seveneight.baijiesteward.ui.user.presenter.AddCardPresenter;
import com.seveneight.baijiesteward.ui.user.presenter.BranchBankPresenter;
import com.seveneight.baijiesteward.ui.user.presenter.EditCardPresenter;
import com.seveneight.baijiesteward.utils.BankCarLegal;
import com.seveneight.baijiesteward.utils.PhoneFormatCheckUtils;
import com.seveneight.baijiesteward.utils.ToastUtil;
import com.seveneight.baijiesteward.views.BranchBankListDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;

/**
 * Created by maxi on 2018/4/14.
 */

public class EditCardActivity extends BaseActivity implements View.OnClickListener, BranchBankContract.BranchBankView,
        AddCardContract.AddCardView, EditCardContract.EditCardView {
    BranchBankContract.BranchBankPresenter branchBankPresenter;
    EditCardContract.EditCardPresenter editCardPresenter;
    AddCardContract.AddCardPresenter addCardPresenter;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_cardnum)
    EditText et_cardnum;
    @BindView(R.id.tv_addcard)
    TextView tv_addcard;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_province)
    TextView tv_province;
    @BindView(R.id.tv_city)
    TextView tv_city;
    @BindView(R.id.tv_bankName)
    TextView tv_bankName;
    @BindView(R.id.tv_bankSmallName)
    TextView tv_bankSmallName;
    private BranchBankListDialog branchBankListDialog;
    private int isDefault = 0;
    private CardBean cardBean;
    private List<BranchBean> provinceData;
    private List<BranchBean> cityData;
    private List<BranchBean> bankNameData;
    private List<BranchBean> bankSmallNameData;
    private int requestType = 1;
    private BranchBean bankNameBean;
    private BranchBean smallBankBean;
    private String bankId = "";
    private String bank = "";
    private String bankNum = "";
    private BranchBean cityBean;
    private BranchBean provinceBean;
    private String branchBank;
    private String branchBankCode;
    private String povince;
    private String provinceId;
    private String city;
    private String cityId;


    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_addcar);
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        tv_title.setText("编辑银行卡");
//        EventBus.getDefault().register(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            tv_title.setText("编辑银行卡");
            cardBean = (CardBean) bundle.getSerializable("data");

            et_name.setText(cardBean.getRealName());
            et_name.setEnabled(false);
            et_cardnum.setText(cardBean.getBankCardNo());
            et_cardnum.setEnabled(false);
            et_phone.setText(cardBean.getBankPhone());
            et_phone.setEnabled(false);


            povince = cardBean.getProvinceName();
            provinceId = cardBean.getProvinceId();
            if (!TextUtils.isEmpty(povince) && povince != null) {
                tv_province.setText(povince);
                provinceBean = new BranchBean();
                provinceBean.setKey(provinceId);
                provinceBean.setText(povince);
            }

            city = cardBean.getCityName();
            cityId = cardBean.getCityId();
            if (!TextUtils.isEmpty(city) && city != null) {
                tv_city.setText(city);
                cityBean = new BranchBean();
                cityBean.setKey(cityId);
                cityBean.setText(city);
            }

            bankId = cardBean.getBankId();
            bank = cardBean.getBankName();
            if (!TextUtils.isEmpty(bank) && bank != null) {
                tv_bankName.setText(bank);
                bankNameBean = new BranchBean();
                bankNameBean.setKey(bankId);
                bankNameBean.setText(bank);
            }

            branchBank = cardBean.getBranchBankName();
            branchBankCode = cardBean.getBranchBankCode();
            if (!TextUtils.isEmpty(branchBank) && branchBank != null) {
                tv_bankSmallName.setText(branchBank);
                smallBankBean = new BranchBean();
                smallBankBean.setCode(branchBankCode);
                smallBankBean.setText(branchBank);
            }

            isDefault = cardBean.getIsDefault() ? 1 : 0;
        }

        editCardPresenter = new
                EditCardPresenter(this);

        addCardPresenter = new
                AddCardPresenter(this);

        branchBankPresenter = new
                BranchBankPresenter(this);
        requestType = 1;
        branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, null, null, null));
    }

    @Override
    protected void setListener() {
        tv_addcard.setOnClickListener(this);
        tv_bankName.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        tv_province.setOnClickListener(this);
        tv_city.setOnClickListener(this);
        tv_bankName.setOnClickListener(this);
        tv_bankSmallName.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_addcard:
                addCard();
                break;
            case R.id.tv_province:
                branchBankListDialog = new BranchBankListDialog(activity, 11, provinceData);
                branchBankListDialog.show();
                break;
            case R.id.tv_city:
                if (cityData == null) {
                    showDialog("请先选择办卡省份");
                    return;
                }
                branchBankListDialog = new BranchBankListDialog(activity, 22, cityData);
                branchBankListDialog.show();
                break;
            case R.id.tv_bankName:
                if (bankNameData == null) {
                    showDialog("请先选择办卡城市");
                    return;
                }
                branchBankListDialog = new BranchBankListDialog(activity, 33, bankNameData);
                branchBankListDialog.show();
                break;
            case R.id.tv_bankSmallName:
                if (bankSmallNameData == null) {
                    showDialog("请先选择办卡银行");
                    return;
                }
                branchBankListDialog = new BranchBankListDialog(activity, 44, bankSmallNameData);
                branchBankListDialog.show();
                break;
        }
    }

    private void addCard() {
        if (provinceBean == null || cityBean == null || bankNameBean == null || smallBankBean == null) {
            showDialog("请手动选择开户行支行");
            return;
        }
        bankId = bankNameBean.getKey();
        branchBank = smallBankBean.getText();
        branchBankCode = smallBankBean.getCode();
        editCardPresenter.editCard(activity, jsonFormat.editDebitCard(cardBean.getDebitCardID(), cardBean.getRealName(), bankId, cardBean.getBankCardNo(), cardBean.getBankPhone(), isDefault, branchBank, branchBankCode));
    }

    @Subscribe
    public void onEvent(MessageBean messageBean) {
        switch (messageBean.getType()) {
            case 11:
                provinceBean = (BranchBean) messageBean.getData();
                tv_province.setText(provinceBean.getText());
                requestType = 2;
                cityData = null;
                bankNameData = null;
                bankSmallNameData = null;
                cityBean = null;
                bankNameBean = null;
                smallBankBean = null;
                tv_city.setText("");
                tv_bankName.setText("");
                tv_bankSmallName.setText("");
                branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, provinceBean.getKey(), null, null));
                break;
            case 22:
                cityBean = (BranchBean) messageBean.getData();
                tv_city.setText(cityBean.getText());
                requestType = 3;
                bankNameData = null;
                bankSmallNameData = null;
                bankNameBean = null;
                smallBankBean = null;
                tv_bankName.setText("");
                tv_bankSmallName.setText("");
                branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, provinceBean.getKey(), cityBean.getKey(), null));
                break;
            case 33:
                bankNameBean = (BranchBean) messageBean.getData();
                tv_bankName.setText(bankNameBean.getText());
                requestType = 4;
                bankSmallNameData = null;
                smallBankBean = null;
                tv_bankSmallName.setText("");
                branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, provinceBean.getKey(), cityBean.getKey(), bankNameBean.getKey()));
                break;
            case 44:
                smallBankBean = (BranchBean) messageBean.getData();
                tv_bankSmallName.setText(smallBankBean.getText());
                break;
        }
    }

    @Override
    public void rebackStatus() {
        ToastUtil.show(activity, "添加成功");
        setResult(RESULT_OK);
        afterFinish();
    }

    @Override
    public void rebackEditStatus() {
        ToastUtil.show(activity, "修改成功");
        setResult(RESULT_OK);
        afterFinish();
    }

    @Override
    public void reBackCreditCardList(List<BranchBean> data) {
        switch (requestType) {
            case 1:
                provinceData = data;
                if (cityBean != null) {
                    requestType = 2;
                    branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, provinceBean.getKey(), null, null));
                }
                break;
            case 2:
                cityData = data;
                if (bankNameBean != null) {
                    requestType = 3;
                    branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, provinceBean.getKey(), cityBean.getKey(), null));
                }
                break;
            case 3:
                bankNameData = data;
                if (smallBankBean != null) {
                    requestType = 4;
                    branchBankPresenter.getBankList(activity, jsonFormat.getBranchBank(requestType, provinceBean.getKey(), cityBean.getKey(), bankNameBean.getKey()));
                }
                break;
            case 4:
                bankSmallNameData = data;
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }
}
